﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ChinaCustoms.Qingdao.Workflow.Supports;
using System.Collections.Generic;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;
using ChinaCustoms.Qingdao.Workflow.Permissions;

public partial class HandoverWorkItem : System.Web.UI.Page
{
    Guid taskGuid = Guid.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        ExecutingSubFlowsRepeater.ItemDataBound += new RepeaterItemEventHandler(ExecutingSubFlowsRepeater_ItemDataBound);
        ExecutingSubFlowsRepeater.ItemCommand += new RepeaterCommandEventHandler(ExecutingSubFlowsRepeater_ItemCommand);

        ExecutingWorkItemsRepeater.ItemDataBound += new RepeaterItemEventHandler(ExecutingWorkItemsRepeater_ItemDataBound);
        ExecutingWorkItemsRepeater.ItemCommand += new RepeaterCommandEventHandler(ExecutingWorkItemsRepeater_ItemCommand);
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["TaskGuid"]))
                taskGuid = new Guid(Request.QueryString["TaskGuid"]);

            if (taskGuid != Guid.Empty)
            {
                TaskInfo currentTask = Tasks.GetTask(taskGuid);
                PageTitle.Text = string.Format("工作移交（{0}）", currentTask.TaskName);

                List<WorkItemInfo> executingWorkItems = WorkItems.GetWorkItems(taskGuid, WorkItemStatus.Executing);
                ExecutingWorkItemsRepeater.DataSource = executingWorkItems;
                ExecutingWorkItemsRepeater.DataBind();

                List<SubTaskInfo> executingSubTasks = SubTasks.GetSubTasks(taskGuid, SubTaskStatus.Executing);
                ExecutingSubFlowsRepeater.DataSource = executingSubTasks;
                ExecutingSubFlowsRepeater.DataBind();
            }
        }

    }


    void ExecutingSubFlowsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.AlternatingItem:
            case ListItemType.Item:
                SubTaskInfo subFlow = e.Item.DataItem as SubTaskInfo;
                if (subFlow != null)
                {
                    Literal subFlowName = e.Item.FindControl("SubFlowName") as Literal;
                    if (subFlowName != null)
                        subFlowName.Text = subFlow.SubTaskName;

                    Literal organizationName = e.Item.FindControl("OrganizationName") as Literal;
                    if (organizationName != null)
                        organizationName.Text = subFlow.OrganizationName;

                    Literal sponsorFullPath = e.Item.FindControl("SponsorFullPath") as Literal;
                    if (sponsorFullPath != null)
                        sponsorFullPath.Text = subFlow.SponsorFullPath;

                    Literal createDate = e.Item.FindControl("CreateDate") as Literal;
                    if (createDate != null)
                        createDate.Text = subFlow.CreateDate.ToString();

                    LinkButton handoverButton = e.Item.FindControl("HandoverButton") as LinkButton;
                    if (handoverButton != null)
                    {
                        handoverButton.CommandArgument = subFlow.SubTaskGuid.ToString();
                        handoverButton.Attributes["onclick"] = "if (!confirm(\"确定要把该子流程移交给别人吗？\")) return false;";
                    }
                }
                break;
        }
    }

    void ExecutingSubFlowsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Guid subTaskGuid = new Guid(e.CommandArgument.ToString());
        switch (e.CommandName)
        {
            case "Handover":
                TextBox handoverToUserName = e.Item.FindControl("HandoverToUserName") as TextBox;
                if (handoverToUserName != null)
                {
                    IUser handoverToUser = ChinaCustomsPermissionWrapper.GetUser(handoverToUserName.Text.Trim());
                    if (handoverToUser != null)
                    {
                        SubTaskInfo subTask = SubTasks.GetSubTask(subTaskGuid);
                        subTask.SponsorUserName = handoverToUser.LogOnName;
                        subTask.SponsorDisplayName = handoverToUser.DisplayName;
                        subTask.SponsorFullPath = handoverToUser.FullPath;
                        SubTasks.UpdateSubTask(subTask);
                        Response.Redirect(Request.RawUrl, true);
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), this.GetType().FullName, "alert(\"用户（"+handoverToUserName.Text.Trim()+"）不存在，请重新输入\")", true);
                    }

                }
                break;
        }
    }

    void ExecutingWorkItemsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.AlternatingItem:
            case ListItemType.Item:
                WorkItemInfo item = e.Item.DataItem as WorkItemInfo;
                if (item != null)
                {
                    Literal activityName = e.Item.FindControl("ActivityName") as Literal;
                    if (activityName != null)
                        activityName.Text = item.ActivityName;


                    Literal creatorFullPath = e.Item.FindControl("CreatorFullPath") as Literal;
                    if (creatorFullPath != null)
                    {
                        creatorFullPath.Text = item.CreatorFullPath;
                    }

                    Literal createDate = e.Item.FindControl("CreateDate") as Literal;
                    if (createDate != null)
                    {
                        createDate.Text = item.CreateDate.ToString();
                    }

                    Literal executorFullPath = e.Item.FindControl("ExecutorFullPath") as Literal;
                    if (executorFullPath != null)
                    {
                        executorFullPath.Text = item.ExecutorFullPath;
                    }

                    LinkButton handoverButton = e.Item.FindControl("HandoverButton") as LinkButton;
                    if (handoverButton != null)
                    {
                        handoverButton.CommandArgument = item.WorkItemGuid.ToString();
                        handoverButton.Attributes["onclick"] = "if (!confirm(\"确定要把该工作项移交给别人吗？\")) return false;";
                    }

                }
                break;
        }
    }


    void ExecutingWorkItemsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Guid  workItemGuid = new Guid(e.CommandArgument.ToString());
        switch (e.CommandName)
        {
            case "Handover":
                TextBox handoverToUserName = e.Item.FindControl("HandoverToUserName") as TextBox;
                if (handoverToUserName != null)
                {
                    IUser handoverToUser = ChinaCustomsPermissionWrapper.GetUser(handoverToUserName.Text.Trim());
                    if (handoverToUser != null)
                    {
                        WorkItemInfo workItem = WorkItems.GetWorkItem(workItemGuid);
                        workItem.ExecutorUserName = handoverToUser.LogOnName;
                        workItem.ExecutorDisplayName = handoverToUser.DisplayName;
                        workItem.ExecutorFullPath = handoverToUser.FullPath;
                        WorkItems.SaveWorkItem(workItem);
                        Response.Redirect(Request.RawUrl, true);
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), this.GetType().FullName, "alert(\"用户（" + handoverToUserName.Text.Trim() + "）不存在，请重新输入\")", true);
                    }
                }
                break;
        }
    }


}
